var PATH_PREFIX ="";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	
	WASP_WIDGET.triggerSelectOnEmpId("#createId", {queryDimission : true,cleanDele: true});
	
	WASP_WIDGET.triggerDateRangeStyle("createTime");
	
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    
    INVESTMENT_RESEARCH_INFO.loadInvestmentResearchData('1', 1, 'InnerInvestmentIesearch');
});

var INVESTMENT_RESEARCH_INFO = {
	changeType: function (researchType) {
		$("#researchType").val(researchType);
		if (researchType == '2') {
			$("#innerAddDiv").hide();
			$("#outAddDiv").show();
		} else {
			$("#outAddDiv").hide();
			$("#innerAddDiv").show();
		}
		queryByCondtion();
	},
	
	loadInvestmentResearchData: function(researchType, pageNo, container) {
		var researchTitle = $.trim($("#researchTitle").val());   
	    var createId = $.trim($("#createId").val());  
	    var createTime0 = $("#createTime").getDateRangeValue()[0]; 
	    var createTime1 = $("#createTime").getDateRangeValue()[1]; 
	    
	    var postData = {
	    	limit: 20,
			pageNo: pageNo,
			'sp[researchType]': researchType,
			'sp[researchTitle]':researchTitle,
	        'sp[createId]':createId,
	        'sp[createTime0]':createTime0,
	        'sp[createTime1]':createTime1
	    }
		
		$.ajax({
			type : "POST",
			dataType : "json",
			url : PATH_PREFIX + 'investmentResearchListPage.do',
			cache: false,
			data: postData,
			success: function(data) {
				if (data.success) {
					// 初始化到页面上
					var $tpl = $('#researchTemplate').html();
					
					$.each(data.items, function(i, item) {
						var createTime = item.createTime;
						item.DD = moment(createTime).format('DD');
						item.YYYYMM = moment(createTime).format('YYYY-MM');
					});
					
					var view = {
						result: data.items
					}
					
					
				    $('#' + container + 'Grid .list-group').html(Mustache.render($tpl, view));
					
					// 重新初始化分页器
				    if (data.itemSize > 0) {
				    	$('#' + container + 'Paginator').show();
				    	$('#' + container + 'Paginator').bootstrapPaginator({
				    		currentPage: data.pageCount < pageNo ? data.pageCount : pageNo,
				    				totalPages: data.pageCount,
				    				size:"normal",
				    				bootstrapMajorVersion: 3,
				    				alignment:"right",
				    				numberOfPages:5,
				    				itemTexts: function (type, page, current) {
				    					switch (type) {
				    					case "first": return "首页";
				    					case "prev": return "上一页";
				    					case "next": return "下一页";
				    					case "last": return "末页";
				    					case "page": return page;
				    					}
				    				},
				    				onPageClicked: function (event, originalEvent, type, page){
				    					INVESTMENT_RESEARCH_INFO.loadInvestmentResearchData(researchType, page, container);
				    				}
				    	});
				    } else {
				    	// 隐藏分页器
				    	$('#' + container + 'Paginator').hide();
				    }
				}
			}
		});
	
	},
	
	// 新增
	showInvestmentResearchAddView: function() {
		var researchType = $("#researchType").val();
		var actionUrl = PATH_PREFIX + "investmentResearchAddView.do?researchType=" + researchType;
		window.open(actionUrl);
	},
	deleteInvestmentResearch: function(id) {
    	ctools.confirm(
    		{title : "确认删除？", text: ""},
			function(isConfirm){
				if(isConfirm){
					$.ajax({
						type : "POST",
						dataType : "json",
						url : PATH_PREFIX + "deleteInvestmentResearch.do",
						cache: false,
						data: {
							'id': id
						},
						success: function(data) {
							swal("删除成功", "", "success");
							queryByCondtion(false);
						},
						error:function(xhr){
						   sweetAlertAjaxError(xhr);
						}
					});
				}
			}
    	);
    }
};

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion();
	}
});

function queryByCondtion() {
	var researchType = $("#researchType").val();
	INVESTMENT_RESEARCH_INFO.loadInvestmentResearchData(researchType, 1, 'InnerInvestmentIesearch');
};